package kr.hkit.androidcomplete03.ch20;

import kr.hkit.androidcomplete03.R;
import kr.hkit.androidcomplete03.ch20.FragmentTest.CounterFragment;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class FragmentManagerTest extends Activity {
	FragmentManager fm;
	Fragment frag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch20_fragmenttestbtn);
		
		fm = getFragmentManager();
		
	}

	public void mOnClick(View v) {
		frag = fm.findFragmentById(R.id.ch20_frame);
		
		switch (v.getId()) {
		case R.id.ch20_btnadd:
			if(frag == null){
				CounterFragment cf = new CounterFragment();
				fm.beginTransaction().add(R.id.ch20_frame, cf, "counter").commit();
			} else {
				Toast.makeText(this, "이미 있다!", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.ch20_btnremove:
			if(frag == null){
				Toast.makeText(this, "프레그먼트 없다!", Toast.LENGTH_SHORT).show();		
			} else {
				fm.beginTransaction().remove(frag).commit();
			}
			break;
		case R.id.ch20_btnreplace:
			if(frag == null){
				Toast.makeText(this, "프레그먼트 없다!", Toast.LENGTH_SHORT).show();		
			} else {
				if(frag.getTag().equalsIgnoreCase("counter")){
					TextFragment tf = new TextFragment();
					fm.beginTransaction().replace(R.id.ch20_frame, tf, "text").commit();
				} else {
					CounterFragment cf = new CounterFragment();
					fm.beginTransaction().replace(R.id.ch20_frame, cf, "counter").commit();
				}
			}
			break;
		case R.id.ch20_btnhide:
			if(frag == null){
				Toast.makeText(this, "프레그먼트 없다!", Toast.LENGTH_SHORT).show();		
			} else {
				if(frag.isHidden()){
					fm.beginTransaction().show(frag).commit();
				} else {
					fm.beginTransaction().hide(frag).commit();
				}
			}
			break;
		}
	}
}
